/**
 *  get table columns
 */
jQuery.fn.dataTableExt.oApi.fnGetColumns = function ( oSettings) {
    return oSettings.aoColumns;
};

/**
 *  get table headers
 */
jQuery.fn.dataTableExt.oApi.fnGetHeaders = function ( oSettings) {
    return oSettings.aoHeader;
};

/**
 *  This function is used to update array changed on product list page
 *  When product info is changed, jsonProduct will be updated
 *  jsonProduct as following:
	[
		{"name":"门把手", "status":2, "level":1, "category":111,"product":1},
		{"name":"门锁", "status":null, "level":null, "category":121,"product":2}
	] 
 */

function updateArrayChanged(type, value, el, jsonProduct){
	var isExisting = false,
		idProductChanged = null, 
		elCurrentRow;
	
	elCurrentRow = $(el).closest("tr");
	idProductChanged = $(elCurrentRow).data("id");
	for (var i=0; i< jsonProduct.length;i++){
		if ( idProductChanged == jsonProduct[i].product){
			switch (type){
				case "name":
					jsonProduct[i].name = value;
					break;
				case "status":
					jsonProduct[i].status = value;
					break;
				case "level":
					jsonProduct[i].level = value;
					break;
				case "category":
					jsonProduct[i].category = value;
					break;
				case "delete":
					jsonProduct.splice(i,1);
					break;
			}
			//set isExisting to true if this value already existing in jsonProduct
			isExisting = true;
			break;
		}
	}
	if (!isExisting){
		switch (type){
			case "name":
				jsonProduct.push({"name":value,"status":null,"level":null,"category":null,"product":idProductChanged});
				break;
			case "status":
				jsonProduct.push({"name":null,"status":value,"level":null,"category":null,"product":idProductChanged});
				break;
			case "level":
				jsonProduct.push({"name":null,"status":null,"level":value,"category":null,"product":idProductChanged});
				break;
			case "category":
				jsonProduct.push({"name":null,"status":null,"level":null,"category":value,"product":idProductChanged});
				break;
		}
		isExisting = false;
	}
}

/**
 *  This function is used to update array changed on product detail page
 *  When product property info is changed, jsonProperty will be updated
 *  jsonProperty as following:
	[
		{"name":"门把手", "status":2, "level":1, "category":111,"product":1},
		{"name":"门锁", "status":null, "level":null, "category":121,"product":2}
	] 
 */

function updateArrayChangedProperty(type, value, el, jsonProperty){
	var isExisting = false,
		idPropertyChanged, 
		elCurrentRow;
	
	elCurrentRow = $(el).closest("tr");
	idPropertyChanged = $(elCurrentRow).data("id");
	for (var i=0; i< jsonProperty.length;i++){
		if ( idPropertyChanged == jsonProperty[i].idProperty){
			switch (type){
				case "name":
					jsonProperty[i].name = value;
					break;
				case "value":
					jsonProperty[i].value = value;
					break;
				case "description":
					jsonProperty[i].description = value;
					break;
				case "delete":
					jsonProperty.splice(i,1);
					break;
			}
			//set isExisting to true if this value already existing in jsonProperty
			isExisting = true;
			break;
		}
	}
	if (!isExisting){
		switch (type){
			case "name":
				jsonProperty.push({"name":value,"value":null,"description":null,"idProperty":idPropertyChanged});
				break;
			case "value":
				jsonProperty.push({"name":null,"value":value,"description":null,"idProperty":idPropertyChanged});
				break;
			case "description":
				jsonProperty.push({"name":null,"value":null,"description":value,"idProperty":idPropertyChanged});
				break;
		}
		isExisting = false;
	}
}